#!/usr/bin/env python
# coding=utf-8

import unittest
import time
import os.path
from utility import base
from utility.match import *
from utility.update import *



class Match_answer_fillin(base.BaseTests):

    #数学选择题答题
    def test_asmatch_fillin(self):
        current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
        img_path = os.path.abspath(os.path.join(os.path.join(__file__, '../../log'), '截图'))
        #等待开屏页过去
        time.sleep(3)
        #点击作业列表底部icon
        self.homework_list()

        submitted = False

        question_number = 0

        while not submitted:
            question_number += 1
            #选择题选项
            choice_a = self.driver.find_element_by_xpath(u'//XCUIElementTypeButton[@name="A"]')
            choice_b = self.driver.find_element_by_xpath(u'//XCUIElementTypeButton[@name="B"]')
            next_btn = self.driver.find_element_by_xpath(u'//XCUIElementTypeButton[@name="下一题"]')
            submit_btn = self.driver.find_element_by_xpath(u'//XCUIElementTypeButton[@name="完成提交"]')
            #判断题选项
            choice_da = self.driver.find_element_by_xpath(u'//XCUIElementTypeButton[@name=">"]')
            choice_xiao = self.driver.find_element_by_xpath(u'//XCUIElementTypeButton[@name="<"]')
            choice_deng = self.driver.find_element_by_xpath(u'//XCUIElementTypeButton[@name="="]')
            #判断题
            choice_dui = self.driver.find_element_by_xpath(u'//XCUIElementTypeButton[@name="keyboard correct choice"]')
            choice_cuo = self.driver.find_element_by_xpath(u'//XCUIElementTypeButton[@name="keyboard incorrect choice"]')
            #纯数字题
            choice_dot = self.driver.find_element_by_xpath(u'//XCUIElementTypeButton[@name="."]')
            choice_one = self.driver.find_element_by_xpath(u'//XCUIElementTypeButton[@name="1"]')
            choice_add = self.driver.find_element_by_xpath(u'// XCUIElementTypeButton[ @ name = "+"]')
            next_kong = self.driver.find_element_by_xpath(u'//XCUIElementTypeButton[@name="下一空"]')


            #选择题
            if choice_a and choice_b:
                novice_guide = self.driver.find_element_by_ios_predicate(u'name CONTAINS "知道了"')
                if novice_guide:
                    novice_guide.click()
                    print '处理了新手引导'
                choice_a.click()

                if submit_btn:
                    submit_btn.click()
                    submitted = True
                elif next_btn:
                    next_btn.click()
                    self.driver.save_screenshot(img_path + current_time+'第'+str(question_number) + "题，下一题.png")
                    time.sleep(0.2)
            # 比较大小题
            elif choice_da and choice_xiao and choice_deng:
                choice_da.click()

                if submit_btn:
                    submit_btn.click()
                    submitted = True
                elif next_btn:
                    next_btn.click()
                    self.driver.save_screenshot(img_path + current_time+'第'+str(question_number) + "题，下一题.png")
                    time.sleep(0.2)
            # 判断题
            elif choice_dui and choice_cuo:
                choice_dui.click()

                if submit_btn:
                    submit_btn.click()
                    submitted = True
                elif next_btn:
                    next_btn.click()
                    self.driver.save_screenshot(img_path + current_time+'第'+str(question_number) + "题，下一题.png")
                    time.sleep(0.2)
            #纯数字键盘
            elif choice_one and choice_dot:
                novice_guide = self.driver.find_element_by_ios_predicate(u'name CONTAINS "知道了"')
                if novice_guide:
                    novice_guide.click()
                    print '处理了新手引导'
                choice_one.click()

                if submit_btn:
                    submit_btn.click()
                    submitted = True
                elif next_btn:
                    next_btn.click()
                    # self.driver.save_screenshot(img_path + current_time+'第'+str(question_number) + "题，下一题.png")
                    time.sleep(0.1)
                elif next_kong:
                    next_kong.click()
                    # self.driver.save_screenshot(img_path + current_time + '第' + str(question_number) + "题，下一空.png")
                    time.sleep(0.1)

            #填空加减乘除
            elif choice_add:

                novice_guide = self.driver.find_element_by_ios_predicate(u'name CONTAINS "知道了"')
                if novice_guide:
                    novice_guide.click()
                    print '处理了新手引导'
                choice_add.click()

                if submit_btn:
                    submit_btn.click()
                    submitted = True
                elif next_btn:
                    next_btn.click()
                    # self.driver.save_screenshot(img_path + current_time+'第'+str(question_number) + "题，下一题.png")
                    time.sleep(0.1)
                elif next_kong:
                    next_kong.click()
                    # self.driver.save_screenshot(img_path + current_time + '第' + str(question_number) + "题，下一空.png")
                    time.sleep(0.1)

            else:
                self.driver.save_screenshot(img_path + current_time + '第' + str(question_number) + "题，不是选择题+条件不满足.png")
                self.assertTrue(False, u"第 %d 道题条件不满足" % question_number)



if __name__ == '__main__':
    test_suite = unittest.TestLoader().loadTestsFromTestCase(Match_answer_fillin)
    unittest.TextTestRunner().run(test_suite)
